package com.example.springsecurity.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/fileDownLoad")
public class FileDownloadController {
    @Value("${file.serverPath}")
    private String fileServerPath;

    @GetMapping("/fromUrlToLocalPath")
    public String fromUrlToLocalPath() throws Exception {
        //因为是测试，图片url可以随便搞一个，我从百度随便复制了个图片url
        String pictureUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnimg.ws.126.net%2F%3Furl%3Dhttp%253A%252F%252Fdingyue.ws.126.net%252F2021%252F0812%252Fc8eb08e2j00qxqd8w0057c0012w00obg.jpg%26thumbnail%3D650x2147483647%26quality%3D80%26type%3Djpg&refer=http%3A%2F%2Fnimg.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651129353&t=f034e15dc07748726fa861080938ec36";
        //建立图片连接
        URL url = new URL(pictureUrl);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        //设置请求方式
        connection.setRequestMethod("GET");
        //设置超时时间
        connection.setConnectTimeout(10*1000);

        //输入流
        InputStream stream = connection.getInputStream();
        int len = 0;
        byte[] test = new byte[1024];


        //获取项目路径
        File directory = new File("src/main/resources/static/images");
        String paths = directory.getCanonicalPath(); //返回文件的绝对路径
        //如果没有文件夹则创建
        File file = new File(paths);
        if (!file.exists()){
            file.mkdirs();
        }

        //设置图片名称，这个随意，我是用的当前时间命名
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String fileName = date + ".png";

        //输出流，图片输出的目的文件
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(paths +"/" + fileName));

        //以流的方式上传
        while ((len =stream.read(test)) !=-1){
            fos.write(test,0,len);
        }


        //记得关闭流，不然消耗资源
        stream.close();
        fos.close();
        return paths;
    }

    @GetMapping("/fromUrlToServerPath")
    public String fromUrlToServerPath() throws Exception {
        //因为是测试，图片url可以随便搞一个，我从百度随便复制了个图片url
        String pictureUrl = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fnimg.ws.126.net%2F%3Furl%3Dhttp%253A%252F%252Fdingyue.ws.126.net%252F2021%252F0812%252Fc8eb08e2j00qxqd8w0057c0012w00obg.jpg%26thumbnail%3D650x2147483647%26quality%3D80%26type%3Djpg&refer=http%3A%2F%2Fnimg.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651129353&t=f034e15dc07748726fa861080938ec36";
        //建立图片连接
        URL url = new URL(pictureUrl);
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        //设置请求方式
        connection.setRequestMethod("GET");
        //设置超时时间
        connection.setConnectTimeout(10*1000);

        //输入流
        InputStream stream = connection.getInputStream();
        int len = 0;
        byte[] test = new byte[1024];


        //获取项目路径
        File directory = new File(fileServerPath);
        String paths = directory.getCanonicalPath(); //返回文件的绝对路径
        //如果没有文件夹则创建
        File file = new File(paths);
        if (!file.exists()){
            file.mkdirs();
        }

        //设置图片名称，这个随意，我是用的当前时间命名
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
        String date = dateFormat.format(new Date());
        String fileName = date + ".png";

        //输出流，图片输出的目的文件
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(paths +"/" + fileName));

        //以流的方式上传
        while ((len =stream.read(test)) !=-1){
            fos.write(test,0,len);
        }


        //记得关闭流，不然消耗资源
        stream.close();
        fos.close();
        return paths;
    }
}
